package br.com.carrosvale.carga;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.exception.ConstraintViolationException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:servlet-context.xml"})
public class MarcaCarga {

	private SessionFactory sessionFactory; 

	@Test
	public void cadastrar() throws Exception{
		
		String cidadeSql = getClass().getClassLoader().getResource("marcas.sql").getFile();
		
        BufferedReader in = new BufferedReader(new FileReader(cidadeSql));

        List<String> querys = new ArrayList<String>();
        
        while (in.ready()) {
        	querys.add(in.readLine());
        }
        
        in.close();

        Session session = sessionFactory.openSession();
        
        for( String sql : querys ){
        	try{
        		session.createSQLQuery(sql).executeUpdate();
        	}catch(ConstraintViolationException e ){
        		
        	}
        }
        
        session.close();
	}

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	
}
